package rcs34.android.Namedays.Activity;

import rcs34.android.Namedays.GApp;
import rcs34.android.Namedays.PreferenceColor;
import rcs34.android.Namedays.R;
import rcs34.android.Namedays.Widget.WidgetProvider;
import android.app.AlertDialog;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class A_Preferences extends PreferenceActivity implements
		OnSharedPreferenceChangeListener, DialogInterface.OnClickListener {
	public static SharedPreferences prefs;
	private AlertDialog alertDialog;
	private PreferenceManager pm = null;
	private int wId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		construct(getIntent());
	}

	private void construct(Intent _intent) {
		int newWId = _intent.getIntExtra(GApp.EXTRA_WID,
				AppWidgetManager.INVALID_APPWIDGET_ID);
		if (newWId != AppWidgetManager.INVALID_APPWIDGET_ID) {
			if (newWId != wId) {
				wId = newWId;
				pm = getPreferenceManager();
				if (GApp.isPro) {
					pm.setSharedPreferencesName(GApp.getWidgetPrefix(newWId));
				}
				prefs = pm.getSharedPreferences();
				Editor editor = prefs.edit();
				if (!prefs.contains(getString(R.string.prefDisplayDate))) {
					editor.putBoolean(getString(R.string.prefDisplayDate),
							GApp.dDisplayDate);
					editor.putString(getString(R.string.prefFontSize),
							GApp.dFontSize);
					editor.putString(getString(R.string.prefADayColorFont),
							GApp.dcADayFont);
					editor.putString(getString(R.string.prefADayColorTop),
							GApp.dcADayTop);
					editor.putString(getString(R.string.prefADayColorBottom),
							GApp.dcADayBottom);
					editor.putString(getString(R.string.prefNDayColorFont),
							GApp.dcNDayFont);
					editor.putString(getString(R.string.prefNDayColorTop),
							GApp.dcNDayTop);
					editor.putString(getString(R.string.prefNDayColorBottom),
							GApp.dcNDayBottom);
					editor.putString(getString(R.string.prefCalendar),
							getString(R.string.dTable));
					editor.putString(getString(R.string.prefLinesPerDate),
							getString(R.string.dLinesPerDate));
				}
				editor.commit();
				editor = null;
				// Set actual values
				setCBValue(getString(R.string.prefDisplayFlag));
				setCBValue(getString(R.string.prefDisplayDate));
				setCBValue(getString(R.string.prefUseTransparency));
				setListValue(getString(R.string.prefFontSize));
				setColorValue(getString(R.string.prefADayColorFont));
				setColorValue(getString(R.string.prefADayColorTop));
				setColorValue(getString(R.string.prefADayColorBottom));
				setColorValue(getString(R.string.prefNDayColorFont));
				setColorValue(getString(R.string.prefNDayColorTop));
				setColorValue(getString(R.string.prefNDayColorBottom));
				setListValue(getString(R.string.prefCalendar));
				setListValue(getString(R.string.prefLinesPerDate));
				// Nice simaries
				ListPreference listPref;
				listPref = (ListPreference) findPreference(getString(R.string.prefFontSize));
				listPref.setSummary(listPref.getEntry());
				listPref = (ListPreference) findPreference(getString(R.string.prefCalendar));
				listPref.setSummary(listPref.getEntry());
				listPref = (ListPreference) findPreference(getString(R.string.prefLinesPerDate));
				listPref.setSummary(listPref.getEntry());
			}
		} else {
			alertDialog = new AlertDialog.Builder(this).create();
			alertDialog.setTitle(getString(R.string.WidgetUnknown));
			alertDialog.setMessage(getString(R.string.CallMeFromWidgetPlease));
			alertDialog.setButton(Dialog.BUTTON_POSITIVE, "OK", this);
			alertDialog.show();
		}
	}

	private void setCBValue(String _key) {
		CheckBoxPreference cbPref;
		cbPref = (CheckBoxPreference) findPreference(_key);
		cbPref.setChecked(prefs.getBoolean(_key, false));
	}

	private void setListValue(String _key) {
		ListPreference listPref;
		listPref = (ListPreference) findPreference(_key);
		listPref.setValue(prefs.getString(_key, ""));
	}

	private void setColorValue(String _key) {
		PreferenceColor colorPref;
		colorPref = (PreferenceColor) findPreference(_key);
		colorPref.setColor(prefs.getString(_key, ""));
	}

	@Override
	public void onNewIntent(Intent _intent) {
		construct(_intent);
	}

	/*
	 * private boolean shallDie(Intent _intent) { String action =
	 * _intent.getAction(); if (action != null &&
	 * _intent.getAction().equals(GApp.ACTION_DIE)) { finish(); return true; }
	 * return false; }
	 */

	@Override
	public void onResume() {
		super.onResume();
		if (prefs != null) {
			prefs.registerOnSharedPreferenceChangeListener(this);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (prefs != null) {
			prefs.unregisterOnSharedPreferenceChangeListener(this);
		}
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		Intent i = new Intent();
		i.setAction(WidgetProvider.PREF_ACTION);
		sendBroadcast(i);
		Preference pref = findPreference(key);
		if (pref instanceof ListPreference) {
			ListPreference listPref = (ListPreference) pref;
			pref.setSummary(listPref.getEntry());
		}
	}

	public void onClick(DialogInterface dialog, int which) {
		Intent startMain = new Intent(Intent.ACTION_MAIN);
		startMain.addCategory(Intent.CATEGORY_HOME);
		startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		alertDialog.dismiss();
		startActivity(startMain);
	}
}